import hashlib
import os
import sys

# 自动基于当前脚本所在目录，定位 release/DataCollector.exe
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
TARGET_EXE = os.path.join(SCRIPT_DIR, "release", "DataCollector.exe")


def get_sha256(file_path):
    if not os.path.isfile(file_path):
        sys.stderr.write(f"错误: 文件不存在 - {file_path}\n")
        sys.exit(1)
    sha256 = hashlib.sha256()
    try:
        with open(file_path, 'rb') as f:
            while True:
                chunk = f.read(8192)
                if not chunk:
                    break
                sha256.update(chunk)
    except PermissionError:
        sys.stderr.write(f"错误: 无权读取文件 - {file_path}\n")
        sys.exit(1)
    except Exception as e:
        sys.stderr.write(f"错误: {e}\n")
        sys.exit(1)
    print(sha256.hexdigest().lower())


if __name__ == '__main__':
    get_sha256(TARGET_EXE)